package com.wx_shop.api.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wx_shop.api.comm.ServiceCode;
import com.wx_shop.api.domain.ProCategory;
import com.wx_shop.api.domain.ProPrice;
import com.wx_shop.api.domain.ProProduct;
import com.wx_shop.api.domain.ProProductPropRel;
import com.wx_shop.api.domain.gen.ProCategoryCriteria;
import com.wx_shop.api.domain.gen.ProPriceCriteria;
import com.wx_shop.api.domain.gen.ProProductCriteria;
import com.wx_shop.api.mapper.ProCategoryMapper;
import com.wx_shop.api.mapper.ProPriceMapper;
import com.wx_shop.api.mapper.ProProductMapper;
import com.wx_shop.api.mapper.ProProductPropRelMapper;
import com.wx_shop.api.service.ProductService;
import com.wx_shop.api.util.BeanUtil;
import com.wx_shop.api.vo.BaseReq;
import com.wx_shop.api.vo.BaseRes;
import com.wx_shop.api.vo.CategoryVo;
import com.wx_shop.api.vo.DataPageWrapper;
import com.wx_shop.api.vo.ProPriceVo;
import com.wx_shop.api.vo.ProProductPropRelVo;
import com.wx_shop.api.vo.ProductPropVo;
import com.wx_shop.api.vo.ProductPropVo.PropVal;
import com.wx_shop.api.vo.ProductVo;

@Service
public class ProductServiceImpl implements ProductService {

	@Autowired
	private ProProductMapper productMapper;
	
	@Autowired
	private ProCategoryMapper categoryMapper;
	
	@Autowired
	private ProProductPropRelMapper proProductPropRelMapper;
	
	@Autowired
	private ProPriceMapper proPriceMapper;
	
	@Override
	public BaseRes<DataPageWrapper<ProductVo>> fetchPageProduct(BaseReq<ProductVo> baseReq) {

		int mysqlOffset = (baseReq.getCurrPage() - 1) * baseReq.getPageSize();

		ProProductCriteria mProProductCriteria = new ProProductCriteria();
		mProProductCriteria.setOrderByClause(" update_time desc ");
		mProProductCriteria.setMysqlOffset(mysqlOffset);
		mProProductCriteria.setMysqlLength(baseReq.getPageSize());

		ProProductCriteria.Criteria mCriteria = mProProductCriteria.createCriteria();
		if (baseReq.getData() != null) {
			ProductVo mProductVO = baseReq.getData();
			if (StringUtils.isNotEmpty(mProductVO.getTitle())) {
				mCriteria.andTitleLike(mProductVO.getTitle() + "%");
			}
			
			// 分类搜索
			List<Integer> cates = mProductVO.getCates();
			if( cates != null && cates.size() > 0 ) {
				
			}
 		}
		DataPageWrapper<ProductVo> pageData = new DataPageWrapper<ProductVo>();

		long totalCount = productMapper.countByExample(mProProductCriteria);
		int totalPage = 0;
		if (totalCount > 0) {
			totalPage = (int) (totalCount % baseReq.getPageSize() == 0 ? totalCount / baseReq.getPageSize() : totalCount
					/ baseReq.getPageSize() + 1);
			List<ProProduct> products = productMapper.selectByExample(mProProductCriteria);

			List<ProductVo> rows = BeanUtil.voConvertList(products, ProductVo.class);
			pageData.setRows(rows);
		}

		pageData.setCurrPage(baseReq.getCurrPage());
		pageData.setPageSize(baseReq.getPageSize());
		pageData.setTotalCount(totalCount);
		pageData.setTotalPage(totalPage);

		BaseRes<DataPageWrapper<ProductVo>> res = new BaseRes<DataPageWrapper<ProductVo>>();
		res.setData(pageData);
		res.setServiceCode(ServiceCode.SUCCESS);
		return res;
	}

	@Override
	public BaseRes<List<CategoryVo>> categoryList() {
		
		/**
		 * 递归表内容， 全部查出，再程序处理
		 */
		ProCategoryCriteria mProCategoryCriteria = new ProCategoryCriteria();
		mProCategoryCriteria.createCriteria().andStatusEqualTo(1);
		List<ProCategory> cates = categoryMapper.selectByExample(mProCategoryCriteria);
		
		BaseRes<List<CategoryVo>> res = new BaseRes<List<CategoryVo>>();
		if( cates.size() > 0 ) {
			
			List<CategoryVo> cateVos = BeanUtil.voConvertList(cates, CategoryVo.class, CategoryVo.ignoreList());
			
			/**
			 * 将内容转化为树
			 */
			List<CategoryVo> rCates = new ArrayList<CategoryVo>();
			for( int i=0; i<cateVos.size(); i++ ) {
				if( cateVos.get(i).getParentId() == null || cateVos.get(i).getParentId() < 1) {
					rCates.add(cateVos.get(i));
					cateVos.get(i).setChirlds(genTree(cateVos, cateVos.get(i)));
				}
			}
			res.setData(cateVos);
		}
		res.setServiceCode(ServiceCode.SUCCESS);
		return res;
	}
	
	private List<CategoryVo> genTree(List<CategoryVo> cates, CategoryVo parentCate) {
		List<CategoryVo> chirlds = new ArrayList<CategoryVo>();
		for( int i=0; i<cates.size(); i++ ) {
			if(cates.get(i).getParentId() == parentCate.getId() ) {
				chirlds.add(cates.get(i));
				cates.get(i).setChirlds(genTree(cates, cates.get(i)));
			}
		}
		return chirlds;
	}

	@Override
	public BaseRes<ProductVo> productDetail(Integer productId) {
		
		ProProduct mProProduct = productMapper.selectByPrimaryKey(productId);
		
		BaseRes<ProductVo> res = new BaseRes<ProductVo>();
		if( null != mProProduct ) {
			res.setData(BeanUtil.voConvert(mProProduct, ProductVo.class));
		}
		res.setServiceCode(ServiceCode.SUCCESS);
		return res;
	}

	@Override
	public BaseRes<ProductPropVo> productPrice(Integer productId) {
		
		BaseRes<ProductPropVo> res = new BaseRes<ProductPropVo>();
		res.setData(new ProductPropVo());
		
		// property list
		List<ProProductPropRel> rpprs = proProductPropRelMapper.fetchProductProps(productId);
		List<PropVal> propVals = new ArrayList<PropVal>();
		Map<String, PropVal> propMap = new HashMap<String, PropVal>();
		for( int i=0; i<rpprs.size(); i++ ) {
			ProProductPropRel pppr = rpprs.get(i);
			
			String groupName = pppr.getPropGroupName();
			PropVal propVal = propMap.get(groupName);
			if( propVal == null ) {
				propVal = new PropVal();
				propVal.setValues(new ArrayList<ProProductPropRelVo>());
				propVal.setName(groupName);
				propMap.put(groupName, propVal);
				propVals.add(propVal);
			}
			
			ProProductPropRelVo ppprVo = BeanUtil.voConvert(pppr, ProProductPropRelVo.class);
			propVal.getValues().add(ppprVo);
		}
		res.getData().setPropVals(propVals);
		
		
		// price dic
		ProPriceCriteria mProPriceCriteria = new ProPriceCriteria();
		mProPriceCriteria.createCriteria().andProIdEqualTo(productId);
		List<ProPrice> proPrices = proPriceMapper.selectByExample(mProPriceCriteria);
		
		String minPrice = "0.0";
		String maxPrice = "0.0";
		double _minPrice = 0.0d;
		double _maxPrice = 0.0d;
		
		Map<String, ProPriceVo> skuPriceDic = new HashMap<String, ProPriceVo>();
		res.getData().setSkuPrice(skuPriceDic);
		for( int i=0; i<proPrices.size(); i++ ) {
			ProPrice mProPrice = proPrices.get(i);
			ProPriceVo mProPriceVo = BeanUtil.voConvert(mProPrice, ProPriceVo.class);
			String sku = mProPrice.getSku(); 
			skuPriceDic.put(sku, mProPriceVo);
			
			double nowPrice = Double.valueOf(mProPrice.getPrice());
			if( _minPrice > nowPrice ) {
				_minPrice = nowPrice;
				minPrice = mProPrice.getPrice();
			}
			
			if( _maxPrice < nowPrice ) {
				_maxPrice = nowPrice;
				maxPrice = mProPrice.getPrice();
			}
		}
		
		res.getData().setMinPrice(minPrice);
		res.getData().setMaxPrice(maxPrice);
		
		res.setServiceCode(ServiceCode.SUCCESS);
		
		return res;
	}
	
	public static void main(String[] args) {
		System.out.println("0.9".compareTo("0.8"));
		System.out.println("0.9".compareTo("0.9"));
		System.out.println("0.9".compareTo("0.9"));
	}

}
